package com.lanchetech.admin.controller;

import com.lanchetech.admin.aop.OperatorLog;
import com.lanchetech.admin.service.CategoryService;
import com.lanchetech.bean.request.CategoryReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.CategoryInfoResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.vo.CategoryEditVO;
import com.lanchetech.bean.vo.ShopCategoryEditVO;
import com.lanchetech.entity.Category;
import com.lanchetech.entity.ShopCategory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "类目接口", tags = "类目接口")
@RequestMapping("/api/category")
@RestController
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @ApiOperation("获取类目分页")
    @PreAuthorize("hasAuthority('product.category')")
    @PostMapping("/getCategoryPage")
    public ResultData<BasePageResp<Category>> getCategoryPage(@RequestBody CategoryReq req) {
        return categoryService.getCategoryPage(req);
    }

    @ApiOperation("获取一级二级类目列表")
    @GetMapping("/getTwoLevelCategoryList")
    public ResultData<List<Category>> getTwoLevelCategoryList(@RequestParam("type") Byte type) {
        return categoryService.getTwoLevelCategoryList(type);
    }

    @ApiOperation("获取根类目列表")
    @GetMapping("/getRootCategoryList")
    public ResultData<List<Category>> getRootCategoryList(@RequestParam("type") Byte type) {
        return categoryService.getRootCategoryList(type);
    }

    @ApiOperation("获取所有类目列表")
    @GetMapping("/getAllCategoryList")
    public ResultData<List<Category>> getAllCategoryList(@RequestParam("type") Byte type) {
        return categoryService.getAllCategoryList(type);
    }

    @ApiOperation("获取子类目列表")
    @GetMapping("/getChildrenCategory")
    public ResultData<List<Category>> getChildrenCategory(@RequestParam("id") Long id) {
        return categoryService.getChildrenCategory(id);
    }

    @OperatorLog("添加或编辑类目")
    @ApiOperation("添加或编辑类目")
    @PreAuthorize("hasAuthority('product.category.edit.auth')")
    @PostMapping("/editCategory")
    public BaseResp editCategory(@RequestBody CategoryEditVO categoryEditVO) {
        return categoryService.editCategory(categoryEditVO);
    }

    @ApiOperation("获取类目详情")
    @PreAuthorize("hasAuthority('product.category-edit')")
    @GetMapping("/getCategoryDetail")
    public ResultData<CategoryEditVO> getCategoryDetail(@RequestParam("id") Long id) {
        return categoryService.getCategoryDetail(id);
    }

    @OperatorLog("删除类目")
    @ApiOperation("删除类目")
    @PreAuthorize("hasAuthority('product.category.delete.auth')")
    @GetMapping("/deleteCategory")
    public BaseResp deleteCategory(@RequestParam("id") Long id) {
        return categoryService.deleteCategory(id);
    }

    @ApiOperation("获取类目品牌、销售属性、自然属性")
    @GetMapping("/getCategoryInfo")
    public ResultData<CategoryInfoResp> getCategoryInfo(@RequestParam("id") Long id) {
        return categoryService.getCategoryInfo(id);
    }


    @ApiOperation("获取类目最后一次更新时间")
    @GetMapping("/getCategoryModifyTime")
    public ResultData<Long> getCategoryModifyTime() {
        return categoryService.getCategoryModifyTime();
    }
}
